﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Management.Entity;
namespace Management.ViewModel
{
    public class TreeGrid_Department
    {
        public int DID { get; set; }
        public string DeptName { get; set; }
        public string DeptDescription { get; set; }
        public int? ParentID { get; set; }
        //public bool Checked {get;set;}
        public bool? IsVisible { get; set; }
        public string CreateDate { get; set; }
        public List<TreeGrid_Department> children { get; set; }
        /// <summary>
        /// 菜单
        /// </summary>
        /// <param name="modulelist"></param>
        /// <returns></returns>
        public static List<TreeGrid_Department> Totreetgrid(List<Department> modulelist)
        {
            List<TreeGrid_Department> list = new List<TreeGrid_Department>();
            var pearentmunu = modulelist.Where(q => q.ParentID == 0);
            foreach (var item in pearentmunu)
            {
                TreeGrid_Department tree = new TreeGrid_Department();
                tree.DID = item.DID;
                tree.DeptName = item.DeptName;
                tree.DeptDescription =item.DeptDescription;
                tree.ParentID = item.ParentID;
                tree.CreateDate = item.CreateDate.ToString();
                //tree.Checked = false;
                tree.IsVisible = item.IsVisible;
                var _hasChiled = modulelist.Where(q => q.ParentID == item.DID).FirstOrDefault();
                if (_hasChiled != null)
                {
                    tree.children = AddChildren(item.DID, modulelist);
                }
                
                list.Add(tree);
            }
            return list;
        }

        private static List<TreeGrid_Department> AddChildren(int pid, List<Department> modulelist)
        {
            List<TreeGrid_Department> list = new List<TreeGrid_Department>();
            var pearentmunu = modulelist.Where(q => q.ParentID == pid);
            foreach (var item in pearentmunu)
            {
                TreeGrid_Department tree = new TreeGrid_Department();
                tree.DID = item.DID;
                tree.DeptName = item.DeptName;
                tree.DeptDescription = item.DeptDescription;
                tree.ParentID = item.ParentID;
                tree.CreateDate = item.CreateDate.ToString();

                //tree.Checked = false;
                tree.IsVisible = item.IsVisible;
                var _hasChiled = modulelist.Where(q => q.ParentID == item.DID).FirstOrDefault();
                if (_hasChiled != null)
                {
                    tree.children = AddChildren(item.DID, modulelist);
                }

                list.Add(tree);
            }
            return list;
        }
        
        
    }
}
